﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp4
{
    class Program
    {
        public static Dictionary<int, Student> list = new Dictionary<int, Student>(); 
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("请选择：1、添加学生信息。2、查询学生信息，3、删除学生信息.4、退出");
                int key = int.Parse(Console.ReadLine());
                switch (key)
                {
                    case 1:
                        AddStudent();
                        break;
                    case 2:
                        SelectStudent();
                        break;
                    case 3:
                        RemoveStudent();
                        break;
                    case 4:
                        Console.WriteLine("退出成功！");
                        return;
                    default:
                        Console.WriteLine("输入错误,请重新输入：");
                        break;
                }
            }
        }

        private static void RemoveStudent()
        {
            Console.WriteLine("请输入学号：");
            int num = int.Parse(Console.ReadLine());
            if (!list.ContainsKey(num))
            {
                Console.WriteLine("该学生不存在！");
                return;
            }
            else
            {
                list.Remove(num);
                Console.WriteLine("删除成功！");
            }
        }

        private static void SelectStudent()
        {
            while (true)
            {
                Console.WriteLine("请选择：1、查询所有（按学号排序）2、查询所有（按姓名排序）,3、查询所有（按年龄排序）4、按学号查询（查没有，则打印查无此学生）5、退出");
                int key = int.Parse(Console.ReadLine());
                switch (key)
                {
                    case 1:
                        SelsctAllByNum();
                        break;
                    case 2:
                        SelectByName();
                        break;
                    case 3:
                        SelectByAge();
                        break;
                    case 4:
                        SelectByNum();
                        break;
                    case 5:
                        Console.WriteLine("退出成功！");
                        return;
                    default:
                        Console.WriteLine("输入错误，请重新输入：");
                        break;
                }
            }
        }

        private static void SelectByNum()
        {
            Console.WriteLine("输入学号：");
            int num = int.Parse(Console.ReadLine());
            if (!list.ContainsKey(num))
            {
                Console.WriteLine("找不到这个学生！");
                return;
            }
            else
            {
                Console.WriteLine(list[num]);
            }
        }

        private static void SelectByAge()
        {
            List<Student> alist = new List<Student>();
            foreach (var item in list.Values)
            {
                alist.Add(item);
            }
            alist.Sort(new Age());
            foreach (var item in alist)
            {
                Console.WriteLine(item);
            }
        }

        private static void SelectByName()
        {
            List<Student> alist = new List<Student>();
            foreach (var item in list.Values)
            {
                alist.Add(item);
            }
            alist.Sort(new Name());
            foreach (var item in alist)
            {
                Console.WriteLine(item);
            }
        }

        private static void SelsctAllByNum()
        {
            List<Student> alist = new List<Student>();
            foreach (var item in list.Values)
            {
                alist.Add(item);
            }
            alist.Sort(new Num());
            foreach (var item in alist)
            {
                Console.WriteLine(item);
            }
        }

        private static void AddStudent()
        {
            int num;
            while (true)
            {
                Console.WriteLine("请输入学生学号：");
                num = int.Parse(Console.ReadLine());
                if (list.ContainsKey(num))
                {
                    continue;
                }
                break;
            }
            Console.WriteLine("请输入学生姓名；");
            string name = Console.ReadLine();
            Console.WriteLine("请输入学生年龄：");
            int age = int.Parse(Console.ReadLine());
            Student s = new Student(name,age,num);
            list.Add(num,s);
            Console.WriteLine("添加成功！");
        }
    }
}
